﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataAccess;
using RM2012Service.Business;
using System.Data;

namespace RM2012Service.Controller
{
    public class PermissionCtrl
    {
        PermissionDAO dao;

        public PermissionCtrl()
        {
            dao = new PermissionDAO();
        }

        public PermissionInfo[] GetAllPermission()
        {
            PermissionInfo[] list = null;
            try
            {
                DataTable dt = dao.GetAllPermission();
                if (dt.Rows.Count > 0)
                {
                    list = new PermissionInfo[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list[i] = new PermissionInfo(dt.Rows[i]["UserId"].ToString(),
                            dt.Rows[i]["UserGroupId"].ToString());
                    }
                }
            }
            catch
            {
                list = null;
            }
            return list;
        }

        public bool AddPermission(PermissionInfo info)
        {
            int bAdd = dao.AddPermission(info);
            if (bAdd > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool UpdatePermission(PermissionInfo info)
        {
            int bUpdate = dao.UpdatePermission(info);
            if (bUpdate > 0)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }

        public bool DeletePermissionByUserId(string userId)
        {
            int bDelete = dao.DeletePermissionByUserId(userId);
            if (bDelete > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeletePermissionByUserGroupId(string userGroupId)
        {
            int bDelete = dao.DeletePermissionByUserGroupId(userGroupId);
            if (bDelete > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
